#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
//宏定义
#define high 20
#define width 50
#define EnemyNum 5//敌机数目

//全局变量————————————————
int canvas[high][width];
int position_x,position_y;//飞机位置坐标
int bullet_x,bullet_y;//子弹坐标
int i,j,k;
int enemy_x[EnemyNum],enemy_y[EnemyNum];//敌机坐标
int score;
int BulletWidth;//子弹宽度

//光标移动到（x,y）位置
void gotoxy(int x,int y)   
{	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle,pos);
}

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1,0};  //表示隐藏光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
//数据初始化
void startup()
{
	position_x=high-3;
	position_y=width/2;
	canvas[position_x][position_y]=1;
	
	int k;
	for(k=0;k<EnemyNum;k++)
	{
		enemy_x[k]=rand()%3;
		enemy_y[k]=rand()%width-1;
		canvas[enemy_x[k]][enemy_y[k]]=3;
	}
	BulletWidth=5;
	score=0;
	
}	
//页面显示
void show()
{
	gotoxy(0,0);

	for(i=0;i<high;i++)
	{
		for(j=0;j<width;j++)
		{
			if(canvas[i][j]==1)
				printf("*");//1输出飞机
			else if(canvas[i][j]==2)
				printf("|");//2输出子弹
			else if(canvas[i][j]==3)
				printf("@");//3输出敌机
			else if(i==high-1 || i==0 || j==0 || j==width-1)
				printf("*");//输出边框
			else
				printf(" ");
		}
		printf("\n");
	}
	printf("得分：%d\n",score);
	HideCursor();
}
//与输入无关的
void updateWithoutInput()
{
	for(i=0;i<high;i++)
	{
		for(j=0;j<width;j++)
		{
			if(canvas[i][j]==2)
			{
				for(k=0;k<EnemyNum;k++)
				{
					if(i==enemy_x[k] && j==enemy_y[k])
					{
						enemy_x[k]=0;
						enemy_y[k]=rand()%width;
						canvas[enemy_x[k]][enemy_y[k]]=3;
						score++;
					}
					canvas[i][j]=0;
					if(i>0)
						canvas[i-1][j]=2;//子弹向上移动
				}

			}
		}
	}
	//当敌机跑出下边界时，重新随机生成
	for(k=0;k<EnemyNum;k++)
	{
		if(enemy_x[k]>high)
		{
				canvas[enemy_x[k]][enemy_y[k]]=0;
				enemy_x[k]=0;
				enemy_y[k]=rand()%width;
				canvas[enemy_x[k]][enemy_y[k]]=3;
				score--;
		}
	}
		static int speed=0;
		if(speed<10)
			speed++;
		if(speed==10)
		{
			//敌机自动下落

			for(k=0;k<EnemyNum;k++)
			{		
				canvas[enemy_x[k]][enemy_y[k]]=0;
				enemy_x[k]++;
				canvas[enemy_x[k]][enemy_y[k]]=3;	
			}
			speed=0;
		}
}
  
//与输入有关的
void  updateWithInput()
{
	char input;
	if (kbhit())//按键
	{
		input=getch();
		if(input=='A')
		{
			canvas[position_x][position_y]=0;
			position_y--;
			canvas[position_x][position_y]=1;
		}
		if(input=='D')
		{
			canvas[position_x][position_y]=0;
			position_y++;
			canvas[position_x][position_y]=1;
		}
		if(input=='W')
		{
			canvas[position_x][position_y]=0;
			position_x--;
			canvas[position_x][position_y]=1;
		}
		if(input=='S')
		{
			canvas[position_x][position_y]=0;
			position_x++;
			canvas[position_x][position_y]=1;
		}
		if(input==' ')
		{
			int left,right,x;
			left=position_y-BulletWidth;
			if(left==0)
				left=0;
			right=position_y+BulletWidth;
			if(right>width-1)
				right=width-1;

			for(x=left;x<=right;x++)
				canvas[position_x-1][x]=2;
		}
	}
}
//主函数
int main()
{
	startup();
	while (1)
	{
		show();
		updateWithInput();
		updateWithoutInput();
	}
	return 0;
}